package com.tz.actions;

import com.opensymphony.xwork2.ActionSupport;
import com.tz.dao.ProductDao;
import com.tz.entity.Page;
import com.tz.entity.Picture;
import com.tz.entity.Product;
import com.tz.service.ProductService;
import com.tz.service.ProductServiceMockImpl;
import com.tz.util.AcUtil;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Created by Administrator on 16-7-19.
 */
@Namespace("/product")
public class ProductAction extends ActionSupport {

    @Autowired
    private ProductDao productDao;

    //C==>V
    private Page<Product> page;
    private String categoryId;
    private int pageNumber;
    private String sortBy;
    private String sortOrder;
    private String viewType;
    private Product product;
    private List<Product> products;

    /**
     * 产品详情
     */
    @Action(value = "details", results = {
            @Result(location = "/product_details.jsp")
    })
    public String detail() {
        //从service 查询数据...

        return SUCCESS;
    }

    /**
     * 产品查询
     * @return
     */
    @Action(value = "search", results = {
            @Result(location = "/products.jsp")
    })
    public String search(){
        //查询逻辑...
        //第一次从首页按categoryid查询
        page = productDao.search(categoryId,
                                        pageNumber,
                                        sortBy,
                                        sortOrder,
                                        viewType);
        List<Product> products = page.getData();
        List<Picture> pictures = null;
        for (Product p : products) {
            pictures = productDao.findPictureByProductId(p.getId());
            p.setPictures(pictures);
        }
        return SUCCESS;
    }

    /**
     * 产品列表

    @Action(value = "list", results = {
            @Result(location = "/products.jsp")
    })
    public String list() {
        //从service 查询数据...
        return SUCCESS;
    }
    */

    public Page<Product> getPage() {
        return page;
    }

    public void setPage(Page<Product> page) {
        this.page = page;
    }

    public String getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(String categoryId) {
        this.categoryId = categoryId;
    }

    public int getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
    }

    public String getSortBy() {
        return sortBy;
    }

    public void setSortBy(String sortBy) {
        this.sortBy = sortBy;
    }

    public String getSortOrder() {
        return sortOrder;
    }

    public void setSortOrder(String sortOrder) {
        this.sortOrder = sortOrder;
    }

    public String getViewType() {
        return viewType;
    }

    public void setViewType(String viewType) {
        this.viewType = viewType;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }


    public List<Product> getProducts() {
        return products;
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }
}
